package com.guaoran.study.screenshot;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author : gu cheng
 * @description : 水印
 * @date : 2021/8/13 15:48
 */
public class WatermarkExample {
    // 水印文字大小
    private static final int FONT_SIZE = 28;
    // 水印文字字体
    private static final Font font = new Font("微软雅黑", Font.PLAIN, FONT_SIZE);
    // 水印文字颜色
    private static final Color color = Color.GRAY;
    // 水印透明度
    private static final float alpha = 0.5f;
    // 水印之间的间隔
    private static final int XMOVE = 80;
    // 水印之间的间隔
    private static final int YMOVE = 80;



    public static void main(String[] args) {
        watermark("2222.jpeg","2222.jpeg","1176325113",-45);
    }


    public static void watermark(String srcImgPath, String outImgPath,String watermarkTxt, Integer rotateDegree){
        String suffix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1);
        try {
            // 源图片
            Image srcImg = ImageIO.read(new File(srcImgPath));
            // 原图宽度、高度
            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            // 得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            g.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            // 设置水印文字透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 设置水印文字颜色
            g.setColor(color);
            // 设置水印文字Font
            g.setFont(font);

            //设置水印旋转
            rotateDegree = rotateDegree == null ? 0 : rotateDegree;
            g.rotate(Math.toRadians(rotateDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);


            // 水印设置
            int x = -width / 2;
            int y;
            // 字体长度
            int markWidth = FONT_SIZE * getTextLength(watermarkTxt);
            // 字体高度
            int markHeight = FONT_SIZE;

            // 循环添加水印
            while (x < width * 1.5) {
                y = -height / 2;
                while (y < height * 1.5) {
                    g.drawString(watermarkTxt, x, y);
                    y += markHeight + YMOVE;
                }
                x += markWidth + XMOVE;
            }

            // 释放资源
            g.dispose();
            // 生成图片
            FileOutputStream os = new FileOutputStream(outImgPath);
            ImageIO.write(buffImg, suffix, os);
            os.flush();
            os.close();
            System.out.println("添加水印文字成功!");
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 获取文本长度。汉字为1:1，英文和数字为2:1
     */
    private static int getTextLength(String text) {
        int length = text.length();
        for (int i = 0; i < text.length(); i++) {
            String s = String.valueOf(text.charAt(i));
            if (s.getBytes().length > 1) {
                length++;
            }
        }
        length = length % 2 == 0 ? length / 2 : length / 2 + 1;
        return length;
    }

}
